<?php
// +----------------------------------------------------------------------
// | HisiPHP框架[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2021 http://www.HisiPHP.com
// +----------------------------------------------------------------------
// | HisiPHP承诺基础框架永久免费开源，您可用于学习和商用，但必须保留软件版权信息。
// +----------------------------------------------------------------------
// | Author: 橘子俊 <364666827@qq.com>，开发者QQ群：50304283
// +----------------------------------------------------------------------

namespace app\common\validate;

use app\common\exception\ApiError;
use think\Validate;

class Base extends Validate
{

    /**
     * 自验证
     * @return bool
     * @throws ApiError
     */
    public function goCheck($params = [],$scene = null)
    {
        $params = $params ?: request()->param();

        if (!$this->batch(true)->scene($scene)->check($params)) {
            $msg = is_array($this->error) ? implode('、', $this->error) : $this->error;
            throw new ApiError($msg);
        }
        return true;
    }

    // 正整数验证
    protected function isPositiveInteger($value, $rule='', $data='', $field='')
    {
        if (is_numeric($value) && is_int($value + 0) && ($value + 0) > 0) {
            return true;
        }
        return $field . '必须是正整数';
    }

    // isset 字符串验证
    protected function notEmpty($value, $rule='', $data='', $field='')
    {
        if (empty($value)) {
            return $field . '不允许为空';
        } else {
            return true;
        }
    }
}